<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent1 {
            height: 100px;
            background-color: lightpink;
            text-align: center;
        }
        .parent2{
            height: 50px;
            background-color: red;
            /* 1.父元素设置 width: fit-content; 此时父元素宽度会根据子元素内容多少来显示 */
            width: fit-content;
            /* 2. 然后给父元素设置: margin:auto; 就可以实现水平居中了 */
            margin: auto;
        }
    </style>
</head>

<body>
    <!-- 1. 行内元素水平居中 -->
    <div class="parent1">
        <div class="child1">child1</div>
    </div>

     <div class="parent2">
         <div class="child2">child1</div>
     </div>
</body>

</html>